package com.qf.gh.web.servlet;

import com.alibaba.fastjson.JSON;
import com.qf.gh.domain.Goods;
import com.qf.gh.domain.GoodsType;
import com.qf.gh.domain.PageBean;
import com.qf.gh.domain.User;
import com.qf.gh.service.AdminService;
import com.qf.gh.service.GoodsService;
import com.qf.gh.service.GoodsTypeService;
import com.qf.gh.service.impl.AdminServiceImpl;
import com.qf.gh.service.impl.GoodaServiceImpl;
import com.qf.gh.service.impl.GoodsTypeServiceImpl;
import com.qf.gh.utils.StrUtils;
import org.apache.log4j.lf5.util.StreamUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/adminLogin")
public class AdminLogin extends BaseServlet{
    AdminService adminService=new AdminServiceImpl();
    GoodsTypeService goodsTypeService=new GoodsTypeServiceImpl();
//    GoodsService goodsService=new GoodaServiceImpl();


    public String getpageBean(HttpServletRequest request, HttpServletResponse response){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        request.setAttribute("pageNum",pageNum);
        request.setAttribute("pageSize",pageSize);
        return "forward:/admin/userList.jsp";
    }

    public String aLogin(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StrUtils.isEmpty(username)||StrUtils.isEmpty(password)){
            System.out.println("数据错误");
            return "redirect:/admin/login.jsp";
        }
        List<GoodsType> goodsTypes=goodsTypeService.selectAll();
        request.getSession().setAttribute("goodsTypeList",goodsTypes);
//        List<Goods> goods=goodsService.find();
//        request.getSession().setAttribute("goodsList",goods);
        User admin =adminService.login(username,password);
        request.getSession().setAttribute("admin",admin);
        return "redirect:/admin/login.jsp";
    }
    public String logOut(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("admin");
        request.getSession().invalidate();
        return "redirect:/admin/admin.jsp";
    }
    public void showUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int pageNum1 = 1;
        int pageSize1 = 2;

        if(StrUtils.isNotEmpty(pageNum)) {
            pageNum1 = Integer.parseInt(pageNum);
            if(0==Integer.parseInt(pageNum)){
                pageNum1=1;
            }
        }
        if(StrUtils.isNotEmpty(pageSize)) {
            pageSize1 = Integer.parseInt(pageSize);
        }
        PageBean pageBean =adminService.findByPage( pageNum1, pageSize1);
        request.getSession().setAttribute("pageBean",pageBean);
        List<User> userList=pageBean.getData();
//        List<User> userList=adminService.select(1);
        Object json = JSON.toJSON(userList);
        response.getWriter().print(json);
    }
    public void  searchUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");
        StringBuilder where=new StringBuilder("  WHERE 1=1  ");
        List<Object> parmas=new ArrayList<Object>();
        if(StrUtils.isNotEmpty(username)){
            where.append("  AND  username LIKE ?   ");
            parmas.add("%"+username+"%");
        }
        if(StrUtils.isNotEmpty(gender)){
            where.append("  AND  gender= ? ");
            parmas.add(gender);
        }
        List<User> userList=adminService.selectOne(where.toString(),parmas);
        Object json = JSON.toJSON(userList);
        response.getWriter().print(json);
    }
    public void  deleteUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");
        List<User> users = adminService.selectDelete(Integer.parseInt(id));
        Object json = JSON.toJSON(users);
        response.getWriter().print(json);
    }

    public void showValidUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        List<User> userList=adminService.select(2);
        Object json = JSON.toJSON(userList);
        response.getWriter().print(json);
    }
    public void  allowUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");
        List<User> users = adminService.selectAllow(Integer.parseInt(id));
        Object json = JSON.toJSON(users);
        response.getWriter().print(json);
    }

    public void  searchValidUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");
        StringBuilder where=new StringBuilder("  WHERE 1=1  ");
        List<Object> parmas=new ArrayList<Object>();
        if(StrUtils.isNotEmpty(username)){
            where.append("  AND  username LIKE ?   ");
            parmas.add("%"+username+"%");
        }
        if(StrUtils.isNotEmpty(gender)){
            where.append("  AND  gender= ? ");
            parmas.add(gender);
        }
        List<User> userList=adminService.selectValidOne(where.toString(),parmas,2);
        Object json = JSON.toJSON(userList);
        response.getWriter().print(json);
    }
}
